#include "searchdialog.h"
#include "ui_searchdialog.h"

SearchDialog::SearchDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::SearchDialog)
{
    ui->setupUi(this);
    init();
}

SearchDialog::~SearchDialog()
{
    delete ui;
}

void SearchDialog::search(const QString &searchText)
{
    ui->lineEdit_search->setText(searchText);
    ui->lineEdit_search->returnPressed();
}

void SearchDialog::init()
{
    initPanel();
    // initBody();
    initStyleSheet();
}

#include "pagecontroller.h"
#include "interviewlistwidget.h"
#include "questionlistwidget.h"
void SearchDialog::initStyleSheet()
{
    ui->panel->setStyleSheet(R"(background-color: rgb(247, 249, 252); border-radius: 12px; color: #333333;)");
    ui->head->setStyleSheet(R"(
    #head {
        background-color: transparent;
    })");
    ui->btn_close->setStyleSheet(R"(
    QPushButton {
        background-repeat: no-repeat;
        background-position: center;
        background-image: url(:/ui/btn_close_50.png);
        border: none;
        background-color: transparent;
    }

    QPushButton:hover,
    QPushButton:pressed {
        background-image: url(:/ui/btn_close_100.png);
    })");
    ui->lineEdit_search->setStyleSheet(R"(QLineEdit { padding-left: 30px; border-radius: 15px; border: 1px solid transparent; background: qlineargradient( x1:0, y1:0, x2:1, y2:0, stop:0 #dbeafe, stop:1 #fce7f3 ); color: #4b5563; font-size: 22px; background-repeat: no-repeat; background-position: 10px center; background-origin: content; background-clip: padding; } QLineEdit:!focus { color: #9ca3af; })");

    ui->btn_interview->setStyles(R"(border-radius: 12px; color: #787F8D;)", R"(background-color: transparent; )", R"(background-color: #F9CFE4; )");
    ui->btn_question_cluster->setStyles(R"(border-radius: 12px; color: #787F8D;)", R"(background-color: transparent; )", R"(background-color: #F9CFE4; )");
    ui->btn_question->setStyles(R"(border-radius: 12px; color: #787F8D;)", R"(background-color: transparent; )", R"(background-color: #F9CFE4; )");

    auto controller = new PageController(ui->pages, this);
    auto interviewPage = new InterviewListWidget(ui->pages);
    interviewPage->hideSearchEdit();
    auto id = controller->addPage(ui->btn_interview, interviewPage);
    controller->addPage(ui->btn_question_cluster, new QuestionListWidget(ui->pages));
    controller->addPage(ui->btn_question, new QuestionListWidget(ui->pages));
    controller->switchToPage(id);
}

#include <QGraphicsDropShadowEffect>
void SearchDialog::initPanel()
{
    ui->panel->setFixedSize(1200, 900);

    // 顶层(MainWidget)：无边框 + 透明，仅做容器
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);

    // 给内容面板(panel)加阴影
    auto *effect = new QGraphicsDropShadowEffect(ui->panel);
    effect->setColor(Qt::black);
    effect->setOffset(0, 0); // 0偏移
    effect->setBlurRadius(6); // 模糊半径
    ui->panel->setGraphicsEffect(effect);

    ui->panel->layout()->setSpacing(0);
    ui->panel->layout()->setContentsMargins(0, 10, 0, 10);

    ui->head->layout()->setSpacing(20);
    ui->head->layout()->setContentsMargins (50, 0, 20, 0);
    ui->head->setFixedHeight(60);

    ui->lineEdit_search->setFixedSize(350, 50);
    ui->btn_interview->setFixedSize(60, 40);
    ui->btn_question_cluster->setFixedSize(60, 40);
    ui->btn_question->setFixedSize(60, 40);

    ui->btn_close->setFixedSize(45, 45);
}

#include <QMouseEvent>
void SearchDialog::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        _dragPosition = event->globalPosition().toPoint() - frameGeometry().topLeft();
        _isDraging = true;
        event->accept();
    }
}

void SearchDialog::mouseMoveEvent(QMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && _isDraging) {
        move(event->globalPosition().toPoint() - _dragPosition);
        event->accept();
    }
}

void SearchDialog::mouseReleaseEvent(QMouseEvent *event)
{
    (void)event;
    _isDraging = false;
}

void SearchDialog::on_btn_close_clicked()
{
    SearchDialog::reject();
}

void SearchDialog::on_lineEdit_search_returnPressed()
{
    QString searchText = ui->lineEdit_search->text();
    qDebug() << "搜索: " << searchText;
}


void SearchDialog::on_btn_interview_clicked()
{

}

